package mall.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import mall.pojo.Shop;

@Repository
public class ShopDAOImpl implements ShopDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void createShop(Shop shop) {
		sessionFactory.getCurrentSession().save(shop);
	}

	@Override
	public int updateShop(Shop shop) {
		String hql="UPDATE Shop SET address = :address , description = :description , name = :name , phone = :phone , state = :state , template_id = :template_id WHERE id = :id";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("address", shop.getAddress());
		query.setParameter("description", shop.getDescription());
		query.setParameter("name", shop.getName());
		query.setParameter("phone", shop.getPhone());
		query.setParameter("state",shop.getState());
		query.setParameter("template_id", shop.getTemplateId());
		return query.executeUpdate();
	}

	@Override
	public List<Shop> getListShop(int industryId ) {
		String hql="FROM Shop WHERE industry.id="+industryId;
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return (List<Shop>)query.list();
	}

	@Override
	public Shop getShop(int shopId) {
		String hql="FROM Shop S WHERE S.id="+shopId;
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return (Shop)query.list().get(0);
	}

}
